package initialize

import (
	"context"
	"github.com/go-redis/redis/v8"

	"server/global"

	"go.uber.org/zap"
)

func InitRedis() {
	if global.XidwConfig.System.UseMultipoint || global.XidwConfig.System.UseRedis {
		redisCfg := global.XidwConfig.Redis
		client := redis.NewClient(&redis.Options{
			Addr:     redisCfg.Addr,
			Password: redisCfg.Password,
			DB: redisCfg.DB,
		})
		pong, err := client.Ping(context.Background()).Result()
		if err != nil {
			global.Logger.Error("redis connect ping failed, err:", zap.Error(err))
			panic("redis connect ping failed")
			global.XidwRedis = nil
			return
		} else {
			global.Logger.Info("redis connect ping response:", zap.String("pong", pong))
			global.XidwRedis = client
		}
	} else {
		global.XidwRedis = nil
	}
}
